package generate;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tj.utils.PyUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TestMain {


    public static void readExcel(String excelFilePath) {
        try (FileInputStream fileInputStream = new FileInputStream(new File(excelFilePath));
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            for (Row row : sheet) { // 迭代每一行
                for (Cell cell : row) { // 迭代每一列
                    // 根据不同数据类型，以字符串形式输出数据
                    String cellValue = "";
                    switch (cell.getCellType()) {
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case NUMERIC:
                            cellValue = String.valueOf(cell.getNumericCellValue());
                            break;
                        case BOOLEAN:
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case FORMULA:
                            cellValue = String.valueOf(cell.getCellFormula());
                            break;
                    }
                    System.out.print(cellValue + "\t");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void importDepts(String excelFilePath) {
        try (FileInputStream fileInputStream = new FileInputStream(new File(excelFilePath));
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            String areaName = "";
            for (Row row : sheet) {
                Cell cell = row.getCell(0);
                if(StringUtils.isEmpty(areaName) || !areaName.equals(cell.getStringCellValue())) {
                    areaName = cell.getStringCellValue();
                    // 插入区部门
                }
            }

            /*for (Row row : sheet) { // 迭代每一行
                for (Cell cell : row) { // 迭代每一列
                    // 根据不同数据类型，以字符串形式输出数据
                    String cellValue = "";
                    switch (cell.getCellType()) {
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case NUMERIC:
                            cellValue = String.valueOf(cell.getNumericCellValue());
                            break;
                        case BOOLEAN:
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case FORMULA:
                            cellValue = String.valueOf(cell.getCellFormula());
                            break;
                    }
                    System.out.print(cellValue + "\t");
                }
                System.out.println();
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String filePath = "C:\\Users\\Administrator\\Desktop\\导入\\北关区.xlsx";
        importDepts(filePath);
        // System.out.println(PyUtils.getAllFirstLetter("3单元11号楼"));

    }
}
